package com.love_weinuo.ui.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.view.ContextThemeWrapper;
import android.view.View;
import android.widget.DatePicker;

import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivityCreatePindanBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.dialog.BirthdaySelectDialog;
import com.love_weinuo.util.timeutil.TimeUtil;

import java.util.Calendar;

/**
 * Created by Administrator on 2019/3/2.
 */

public class CreatePindanActivity extends BaseActivity {

    ActivityCreatePindanBinding binding;
    String timeNow ;
    boolean isCheck = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_create_pindan);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setBack();
        setTitleText("发布拼单");
        timeNow = TimeUtil.getNowTime();
        binding.tvModuleNewTime.setText(timeNow.split(" ")[0]);
        binding.tvModuleStopTime.setOnClickListener(this);
        binding.tvModuleServiceClause.setOnClickListener(this);
        binding.tvModuleNext.setOnClickListener(this);
        binding.llModuleRb.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tvModuleStopTime:
                @SuppressLint("RestrictedApi") Context themed = new ContextThemeWrapper(CreatePindanActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog);
                final Calendar calendar = Calendar.getInstance();
                new BirthdaySelectDialog(themed, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int ys, int m, int d) {
                        calendar.set(Calendar.YEAR, ys);
                        calendar.set(Calendar.MONTH, m);
                        calendar.set(Calendar.DAY_OF_MONTH, d);
                        String result = TimeUtil.clanderTodatetime(calendar, "yyyy-MM-dd");
                        result = new StringBuffer(result).append(" 24:59:59").toString();

                        Long createTime = TimeUtil.formatDate(timeNow);
                        Long stopTime = TimeUtil.formatDate(result);
                        LogUtil.e("下单时间==>"+ TimeUtil.getDateTimeFromMillisecond(createTime));
                        LogUtil.e("截止时间==>"+ TimeUtil.getDateTimeFromMillisecond(stopTime));
                        if (createTime > stopTime) {
                            showInCommonDialog("请正确选择时间");
                            return;
                        }
                        binding.tvModuleStopTime.setText(result.split(" ")[0]);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show();
                break;
            case R.id.tvModuleServiceClause:
                ToastUtil.showToast("服务条款");
                break;
            case R.id.tvModuleNext:
                if (!isCheck) {
                    showInCommonDialog("请查看并同意服务条款");
                    return;
                }
                break;
            case R.id.llModuleRb:
                if (isCheck) {
                    isCheck = false;
                    binding.rbModule.setBackgroundResource(R.drawable.draw_service_clause_false);
                }else{
                    isCheck = true;
                    binding.rbModule.setBackgroundResource(R.drawable.draw_service_clause_true);
                }
                break;
                default:
        }
    }
}
